﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Clipper.Model;
using System.Collections.ObjectModel;
using System.Windows;

namespace Communication
{
    public abstract class ControlBase
    {
        #region 变量
        /// <summary>
        /// 控制ID
        /// </summary>
        protected short mCanId;
        /// <summary>
        /// 下层ID，接受参数
        /// </summary>
        protected short mSubCanId;
        protected int mStatus=-100;
        /// <summary>
        /// 等待状态和所发命令对应字典
        /// </summary>
        protected Dictionary<int, string> mSCmd = null;
        protected Dictionary<int, CmdModel> mCmdModelDic = new Dictionary<int, CmdModel>();

        /// <summary>
        /// 超时命令列表
        /// </summary>
        public static ObservableCollection<CmdModel> mSendBuffer = new ObservableCollection<CmdModel>();
        //public static List<CmdModel> mSendBuffer = new List<CmdModel>();
        #endregion

        #region 属性
        /// <summary>
        /// 状态，对应构造函数中设定的命令返回状态，依次为0,1,2,3...
        /// </summary>
        public virtual int Status
        {
            get
            {
                return mStatus;
            }
            set
            {
                mStatus = value;
            }
        }
        private bool mIsPause;
        /// <summary>
        /// 在此处暂停
        /// </summary>
        public bool IsPause
        {
            get
            {
                return mIsPause;
            }
            set
            {
                mIsPause = value;
            }
        }
        private bool mIsTimeout;
        /// <summary>
        /// 是否超时
        /// </summary>
        public bool IsTimeout
        {
            get
            {
                return mIsTimeout;
            }
            set
            {
                mIsTimeout = value;
            }
        }
        private bool mIsDone;
        //执行完的步骤，
        private bool IsDone
        {
            get
            {
                return mIsDone;
            }
            set
            {
                mIsDone=value;
            }
        }
        private ControlStatusEnum mControlStatus;
        /// <summary>
        /// 实例的状态，用于流程控制
        /// </summary>
        public ControlStatusEnum ControlStatus
        {
            get
            {
                return mControlStatus;
            }
            set
            {
                mControlStatus = value;
            }
        }
        #endregion
        /// <summary>
        /// 等待状态改变（等待某个命令的反馈）
        /// </summary>
        /// <param name="status">状态</param>
        public virtual void WaitStatus(int status)
        {
            int time=0;
            while (mStatus != status)
            {
                if (time == 30000)
                {
                    Console.WriteLine(mCanId + "," + mSCmd[status] + "超时");
                    //mSendBuffer.Add(mCmdModelDic[status]);
                 
                    //throw new Exception("超时");
                }
                else if (time == 60000)
                {
                    mIsTimeout = true;

                    if (Application.Current != null)
                    {
                        //Application.Current.Dispatcher.Invoke(new Action(() =>
                        //{
                        //    throw new Exception("fdfsdf");
                        //}));
                    }
                    MessageBox.Show(mCanId+","+ mSCmd[status] + "超时");
                    break;
                }
                Thread.Sleep(100);
                time += 100;
            }

            //while (mStatus != status)
            //{
            //    if (time > 1000)
            //    {
            //        break;
            //    }
            //    Thread.Sleep(100);
            //    time += 100;
            //}

            //暂停
            while (IsPause)
            {
                Thread.Sleep(100);
            }

        }

        /// <summary>
        /// 发送一次数值，向小车下级发送
        /// </summary>
        /// <param name="ed_num">数值</param>
        public CmdModel EncordNum(int ed_num)
        {
            string commandstr = 1+"GD" + ed_num.ToString()+"\r";
            Can.SendCan(Convert.ToInt16(Can.TRANSPORT_INCUBATION_ID_1), commandstr);
            return new CmdModel(Can.TRANSPORT_INCUBATION_ID_1, commandstr);
        }
        /// <summary>
        /// 发送需要等待返回的命令，保存命令以便超时重发
        /// </summary>
        /// <param name="cmdOrder">命令序号</param>
        /// <param name="cmd">命令内容</param>
        public CmdModel SendCmd(int cmdOrder, string cmd)
        {
            string commandstr = cmd + "\r";
            Can.SendCan(Convert.ToInt16(mCanId), commandstr);
            CmdModel _cmdModel = new CmdModel(mCanId, commandstr);
            if (!mCmdModelDic.ContainsKey(cmdOrder))//保存命令
            {
                mCmdModelDic.Add(cmdOrder, _cmdModel);
            }
            return _cmdModel;
        }

        /// <summary>
        /// 发送命令，不需要等待返回
        /// </summary>
        /// <param name="cmd"></param>
        public void SendCmd(string cmd)
        {
            string commandstr = cmd + "\r";
            Can.SendCan(Convert.ToInt16(mCanId), commandstr);
        }
    }
}
